﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;

using DTO;
using Model;

namespace DataService
{
    public class ContactService : BaseService
    {

        ContactModel model = new ContactModel();
        public IList<Contact.ContactType> GetContactTypes ()
        {
            DataTable table = BaseModel.GetAllByTableName("ContactType");
            IList<Contact.ContactType> list = new List<Contact.ContactType>();

            if ( table != null )
            {
                foreach ( DataRow row in table.Rows )
                {
                    list.Add(new Contact.ContactType(BaseModel.GetIntFieldValue(row, "TypeId"))
                    {
                        Name = BaseModel.GetStringFieldValue(row, "TypeName")
                    });
                }
            }

            return list;
        }
        public IList<Contact> GetContacts(int accountId)
        {
            DataTable dtContact = (new AccountModel()).GetListContactByAccountId(accountId);
            IList<Contact> list = new List<Contact>();
            if (dtContact != null && dtContact.Rows.Count > 0)
            {
                foreach (DataRow dr in dtContact.Rows)
                {
                    Contact contact = new Contact(BaseModel.GetIntFieldValue(dr, "ContactId"))
                    {
                        FirstName = BaseModel.GetStringFieldValue(dr, "FirstName"),
                        LastName = BaseModel.GetStringFieldValue(dr, "LastName"),
                        Email = BaseModel.GetStringFieldValue(dr, "Email"),
                        Phone = BaseModel.GetStringFieldValue(dr, "Phone"),
                        Location = LocationInfo.Sample(),
                        Type = new Contact.ContactType(BaseModel.GetIntFieldValue(dr, "ContactTypeId"))
                        {
                            Name = BaseModel.GetStringFieldValue(dr, "ContactTypeName")
                        }
                    };
                    list.Add(contact);
                }
            }
            return list;
        }

        public CreditCard GetCreditCardByAccountId(int AccountId)
        {
            DataRow row = model.GetCreditCardByAccountId(AccountId);
            if (row != null)
            {
                int CardType = ContactModel.GetIntFieldValue(row, "TypeCardId");
                CreditCard card = new CreditCard();
                card.CardHolder = ContactModel.GetStringFieldValue(row, "CardHolder");
                card.Id = AccountId;
                card.Number = ContactModel.GetStringFieldValue(row, "Number");
                card.Type = new CardType()
                {
                    Id = CardType,
                    Name = ContactModel.GetStringFieldValue(row,"TypeCardName")
                };
                card.ExpMonth = ContactModel.GetIntFieldValue(row, "ExpDay");
                card.ExpYear = ContactModel.GetIntFieldValue(row, "ExpYear");
                return card;
            }
            return null;
        }

        public string GetAccountPaymentType(int AccountId)
        {
            DataRow row = model.GetAccountPaymentType(AccountId);
            if (row != null)
            {
                return ContactModel.GetStringFieldValue(row, "Type");
            }
            return null;
        }

        public Contact GetBillingContact(int AccountId)
        {
            DataRow row = model.GetContactBillingAccount(AccountId);
            if (row != null)
            {
                int IdAddress = ContactModel.GetIntFieldValue(row, "AddressId");
                Contact contact = new Contact(IdAddress);
                contact.CompanyName = ContactModel.GetStringFieldValue(row, "CompanyName");
                contact.FirstName = ContactModel.GetStringFieldValue(row, "FirstName");
                contact.LastName = ContactModel.GetStringFieldValue(row, "LastName");
                contact.Email = ContactModel.GetStringFieldValue(row, "Email");
                contact.Phone = ContactModel.GetStringFieldValue(row, "Phone");
                contact.Location = new LocationInfo(IdAddress);
                contact.Location.Address = ContactModel.GetStringFieldValue(row, "Address");
                contact.Location.Street = ContactModel.GetStringFieldValue(row, "Street1");
                contact.Location.City = ContactModel.GetStringFieldValue(row, "City");         
                contact.Location.State = new State();
                contact.Location.State.Code = ContactModel.GetStringFieldValue(row, "StateCode");
                contact.Location.State.Name = ContactModel.GetStringFieldValue(row, "StateName");
                contact.Location.Country = new Country();
                contact.Location.Country.Code = ContactModel.GetStringFieldValue(row, "CountryCode");
                contact.Location.Country.Name = ContactModel.GetStringFieldValue(row, "CountryName");
                contact.Location.ZipCode = ContactModel.GetStringFieldValue(row, "ZipCode");
                return contact;
            }
            return null;
        }

        public void InsertUpdateBillingAddress(int AccountId, Contact contact)
        {
            model.InsertUpdateBillingAddress(AccountId, contact.Id,
                contact.CompanyName, contact.FirstName, contact.LastName, contact.Email, contact.Phone, contact.Location.Address,
                contact.Location.Street, contact.Location.Street, contact.Location.City,
                contact.Location.State.Code, contact.Location.Country.Code, contact.Location.ZipCode);
        }

        public BankAccount GetBankAccountInfo(int AccountId)
        {
            DataRow row = model.GetBankAccountInfoByAccountId(AccountId);
            if (row != null)
            {
                BankAccount bank = new BankAccount();
                bank.AccountNumber = ContactModel.GetStringFieldValue(row, "AccountNumber");
                bank.RountingNumber = ContactModel.GetStringFieldValue(row, "RoutingNumber");
                int bankCategoryId = ContactModel.GetIntFieldValue(row, "BankCategoryId");
                bank.AccountCategory = new AccountCategory(bankCategoryId);
                bank.AccountCategory.Name = ContactModel.GetStringFieldValue(row, "BankCategoryName");
                int bankType = ContactModel.GetIntFieldValue(row, "BankTypeId");
                bank.AccountType = new AccountType(bankType);
                bank.AccountType.Name = ContactModel.GetStringFieldValue(row, "BankTypeName");
                return bank;
            }
            return null;
        }

        public IList<CardType> GetListCardType()
        {
            DataTable dt = model.GetListCardType();
            if (dt != null && dt.Rows.Count > 0)
            {
                IList<CardType> cards = new List<CardType>();
                foreach (DataRow row in dt.Rows)
                {
                    CardType card = new CardType(ContactModel.GetIntFieldValue(row, "TypeId"));
                    card.Name = ContactModel.GetStringFieldValue(row, "TypeName");
                    cards.Add(card);
                }
                return cards;
            }
            return null;
        }

        public IList<AccountType> GetListAccountType()
        {
            DataTable dt = model.GetListBankAccountType();
            if (dt != null && dt.Rows.Count > 0)
            {
                IList<AccountType> Types = new List<AccountType>();
                foreach (DataRow row in dt.Rows)
                {
                    AccountType type = new AccountType(ContactModel.GetIntFieldValue(row, "TypeId"));
                    type.Name = ContactModel.GetStringFieldValue(row, "TypeName");
                    Types.Add(type);
                }
                return Types;
            }
            return null;
        }

        public IList<AccountCategory> GetListAccountCategory()
        {
            DataTable dt = model.GetListBankAccountCategory();
            if (dt != null && dt.Rows.Count > 0)
            {
                IList<AccountCategory> Types = new List<AccountCategory>();
                foreach (DataRow row in dt.Rows)
                {
                    AccountCategory type = new AccountCategory(ContactModel.GetIntFieldValue(row, "TypeId"));
                    type.Name = ContactModel.GetStringFieldValue(row, "TypeName");
                    Types.Add(type);
                }
                return Types;
            }
            return null;
        }

        public Contact GetContact(int contactId)
        {

            DataRow row = model.GetContact(contactId);

            if (row != null)
            {
                return new Contact(contactId)
                {
                    FirstName = BaseModel.GetStringFieldValue(row, "FirstName"),
                    LastName = BaseModel.GetStringFieldValue(row, "LastName"),
                    Phone = BaseModel.GetStringFieldValue(row, "Phone"),
                    Email = BaseModel.GetStringFieldValue(row, "Email"),
                    Type = new Contact.ContactType(BaseModel.GetIntFieldValue(row, "ContactTypeId")) { Name = BaseModel.GetStringFieldValue(row, "ContactTypeName") }
                };
            }

            return null;
        }

        public void InsertUpdateCardAccount(int AccountId, CreditCard credit)
        {
            model.InsertUpdateCardAccount(AccountId, credit.CardHolder, credit.Number, credit.Type.Id, credit.ExpMonth, credit.ExpYear);
        }

        public void InsertUpdateBankAccount(int AccountId, BankAccount bank)
        {
            model.InsertUpdateBankAccount(AccountId, bank.RountingNumber, bank.AccountNumber, bank.AccountType.Id, bank.AccountCategory.Id);
        }

        public void SaveContact(int customerId, int loginId, Contact contact)
        {
            if (contact != null)
            {
                using (ContactModel model = new ContactModel())
                {
                    model.InsertUpdateContact(customerId, loginId, contact.FirstName, contact.LastName, contact.Email, contact.Phone, contact.Type.Id, contact.Id);
                }
            }
        }


        public void InsertUpdateUserInfo(User user)
        {
            if (user != null)
            {
                using (ContactModel model = new ContactModel())
                {
                    model.InsertUpdateUserInfo(user.Customer.Id, user.Id, user.Username, user.Password, user.FirstName, user.LastName, user.Email, user.Phone, 0);
                }
            }
        }

        public void SaveMainContact(Customer customer)
        {
            if (customer != null)
            {
                using (ContactModel model = new ContactModel())
                {
                    Contact contact = customer.MainContact;
                    model.InsertUpdateAccountAddressAll(customer.Id, customer.Code, contact.FirstName, contact.LastName, customer.CompanyName, contact.Email, contact.Phone, contact.Location.Address, contact.Location.Street, "", contact.Location.City, contact.Location.State.Code, contact.Location.Country.Code, contact.Location.ZipCode);
                }
            }
        }

    }
}
